//
//  WalletHeaderView.swift
//  SCA
//
//  Created by tjun on 2025/9/4.
//

import UIKit
import SnapKit


enum ActionButtonType {
    case recharge   // 充值
    case withdraw   // 提款
    case transfer   // 转账
    case exchange   // 兑换
}

struct ActionButtonItem {
    let title: String
    let type: ActionButtonType
}

class WalletHeaderView: UIView {
    
    var onActionButtonTapped: ((ActionButtonType) -> Void)?
    
    var walletInfo: WalletInfo? {
        didSet {
            guard let info = walletInfo else { return }
            totalValueLabel.text = info.total_disp
        }
    }

    var lastView: UIView?
    var stackView: UIStackView!
    
    let tipSet2FAView = GradientTipView(text: "请尽快设置双重验证（2FA），以保障您的资产安全。".localizedString)
    let tipSetPinView = GradientTipView(text: "请尽快设置二级安全密码，以保障您的资产安全。".localizedString)
    
    private let totalLabel = UILabel(text: "总资产".localizedString, font: UIFontMake(14), textColor: .white)
    private let totalValueLabel = UILabel(text: " ", font: UIFontSemiboldMake(36), textColor: .white.withAlphaComponent(0.8))
    
    private let buttonItems: [ActionButtonItem] = [
        ActionButtonItem(title: "充值".localizedString, type: .recharge),
        ActionButtonItem(title: "提款".localizedString, type: .withdraw),
        ActionButtonItem(title: "转账".localizedString, type: .transfer),
        ActionButtonItem(title: "兑换".localizedString, type: .exchange)
    ]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        addSubview(totalLabel)
        addSubview(totalValueLabel)
        
        totalLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(30)
        }
        
        totalValueLabel.snp.makeConstraints { make in
            make.left.equalTo(totalLabel)
            make.top.equalTo(totalLabel.snp.bottom).offset(5)
        }
        
        var buttons: [UIButton] = []
        
        for (index, item) in buttonItems.enumerated() {
            let button = UIButton(type: .system)
            button.setTitle(item.title, for: .normal)
            button.setTitleColor(.white, for: .normal)
            button.backgroundColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
            button.layer.cornerRadius = 8
            button.tag = index
            button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            buttons.append(button)
        }
        
        stackView = UIStackView(arrangedSubviews: buttons)
        stackView.axis = .horizontal
        stackView.spacing = 12
        stackView.alignment = .fill
        stackView.distribution = .fillEqually
        addSubview(stackView)
        
        stackView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().inset(20)
            make.top.equalTo(totalValueLabel.snp.bottom).offset(10)
            make.height.equalTo(30)
        }
        
        let lineView = UIView().addTo(self)
        lineView.backgroundColor = UIColorMake(0xAFB2B7).withAlphaComponent(0.5)
        lineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.7)
        }
        
        lastView = stackView
    }

    func reloadData() {
        
        tipSet2FAView.removeFromSuperview()
        tipSetPinView.removeFromSuperview()
        
        let userInfoModel = UserInfoModel.current
        if !userInfoModel.isSet2fa, !userInfoModel.isSetPin {
            
            tipSet2FAView.addTo(self)
            tipSet2FAView.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom).offset(14)
                make.left.right.equalTo(stackView)
                make.height.equalTo(36)
            }
            
            tipSetPinView.addTo(self)
            tipSetPinView.snp.remakeConstraints { make in
                make.top.equalTo(tipSet2FAView.snp.bottom).offset(10)
                make.left.right.height.equalTo(tipSet2FAView)
            }
            
            lastView = tipSetPinView
            
        } else if !userInfoModel.isSet2fa {
            
            tipSet2FAView.addTo(self)
            tipSet2FAView.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom).offset(14)
                make.left.right.equalTo(stackView)
                make.height.equalTo(36)
            }
            
            lastView = tipSet2FAView
            
        } else if !userInfoModel.isSetPin {
            
            tipSetPinView.addTo(self)
            tipSetPinView.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom).offset(14)
                make.left.right.equalTo(stackView)
                make.height.equalTo(36)
            }
            
            lastView = tipSetPinView
        } else {
            lastView = stackView
            stackView.snp.remakeConstraints { make in
                make.leading.equalToSuperview().offset(20)
                make.trailing.equalToSuperview().inset(20)
                make.top.equalTo(totalValueLabel.snp.bottom).offset(10)
                make.height.equalTo(30)
            }
        }
        
    }
 
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc private func buttonTapped(_ sender: UIButton) {
        let item = buttonItems[sender.tag]
        onActionButtonTapped?(item.type)
    }
    
 
}



class WalletDetailHeaderView: UIView {
    
    var onActionButtonTapped: ((ActionButtonType) -> Void)?

    private let coinLogoImageView = UIImageView()
    private let coinNameLabel = UILabel(text: " ", font: UIFontSemiboldMake(18), textColor: .white)
    private let totalLabel = UILabel(text: "余额".localizedString, font: UIFontMake(12), textColor: .white)
    let totalValueLabel = UILabel(text: "$0.00", font: UIFontSemiboldMake(28), textColor: .white)
    
    private var buttonItems: [ActionButtonItem] = []
    
    let coinModel: WalletCoinModel
    
    init(frame: CGRect, coinModel: WalletCoinModel) {
        self.coinModel = coinModel
        super.init(frame: frame)
        
        coinLogoImageView.setImage(url: coinModel.coinUrl)
        coinNameLabel.text = coinModel.coinName
        totalValueLabel.text = coinModel.value
        
        addSubview(coinLogoImageView)
        coinLogoImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(53)
            make.top.equalTo(20)
        }
        
        addSubview(coinNameLabel)
        coinNameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(coinLogoImageView.snp.bottom).offset(8)
        }
  
        addSubview(totalLabel)
        totalLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(coinNameLabel.snp.bottom).offset(28)
        }
        
        addSubview(totalValueLabel)
        totalValueLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(totalLabel.snp.bottom).offset(5)
        }
        
        if coinModel.transfer {
            buttonItems.append(ActionButtonItem(title: "转账".localizedString, type: .transfer))
        }
        
        if coinModel.withdraw {
            buttonItems.append(ActionButtonItem(title: "提款".localizedString, type: .withdraw))
        }
        
//        ActionButtonItem(title: "转账", type: .transfer),
//        ActionButtonItem(title: "兑换", type: .exchange),
//        ActionButtonItem(title: "提款", type: .withdraw),
        
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 12
        stackView.alignment = .center
        stackView.distribution = .equalSpacing
        addSubview(stackView)
        
        let buttonW = ((screenWidth - AppStyle.margin.left - AppStyle.margin.right) - 3 * stackView.spacing) / 4
        
        for (index, item) in buttonItems.enumerated() {
            let button = UIButton(type: .system)
            button.setTitle(item.title, for: .normal)
            button.setTitleColor(.white, for: .normal)
            button.backgroundColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
            button.layer.cornerRadius = 8
            button.tag = index
            button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            stackView.addArrangedSubview(button)
            button.snp.makeConstraints { make in
                make.width.equalTo(buttonW)
                make.height.equalTo(30)
            }
        }
        
        stackView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(totalValueLabel.snp.bottom).offset(24)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc private func buttonTapped(_ sender: UIButton) {
        let item = buttonItems[sender.tag]
        onActionButtonTapped?(item.type)
    }
    
 
}
